echo "          =========  openwrt_ss begin:      "`date '+%Y.%m.%d %H:%M:%S'`  =====|logger

[ ! "$(grep "option global_server 'nil'" /etc/config/shadowsocksr)" == "" ]  && echo "shadowsocksr is closed!" && exit 1
[ `ps |grep /var/etc/shadowsocksr1.json|grep -v grep|wc -l ` -gt 2 ] && echo "restart shadowsocksr!" && /etc/init.d/shadowsocksr restart &&　exit 1
[ `ps |grep $0|grep -v grep|wc -l ` -gt 2 ] && echo "$0 has already run!" && exit 1
if [ -f /tmp/cron_ss.lock ] ; then
  starttime=$(cat /tmp/cron_ss.lock)
  endtime=$(cat /proc/uptime | cut -d" " -f1)
  TIME=$(awk -v x=$starttime -v y=$endtime 'BEGIN {printf "%2f" y-x}'|awk '{sub(/\./,"",$1); print int($1)}')
  [ $TIME -lt 600 ] && echo "cron_ss.lock found!" && exit 1
  rm /tmp/cron_ss.lock
  /etc/init.d/shadowsocksr restart
fi





serverurl="http://ggcloud.tk/mac/"
url="https://www.youtube.com/intl/zh-CN/yt/about/"

cd /tmp
rm ss.ini >/dev/null 2>&1

######### refresh begin ########
if [ ! "$1"  = "refresh" ]  ; then
	rm /tmp/tmp.txt 2>/dev/null
	curl -o /tmp/tmp.txt -s -k -L --retry 3 -r 0-10239 -m 5 $url 2>/dev/null
	[ ! -s /tmp/tmp.txt ] && curl -o /tmp/tmp.txt -s -k -L --retry 3 -r 0-10239 -m 5 $url 2>/dev/null
	[  -s /tmp/tmp.txt  ]  &&  exit 0
fi
######### refresh end ########

SS0=""
if [ ! -s /tmp/SSJSON.sh ] ; then
	wget -q -O /tmp/SSJSON1.sh  https://gitee.com/yzqiang/autoss/raw/master/SSJSON.sh
	cat SSJSON1.sh |tr -d "\015" >SSJSON.sh
	chmod 777 /tmp/SSJSON.sh
fi

cat > "/tmp/setssr.sh" <<-\SETSSR
base64_str=""
base64_res=""
base64_encode() 
{
vvvvv=`echo -n $base64_str|base64|sed 's/=//g'|sed 's/\//_/g'`
base64_res=`echo $vvvvv|sed s/[[:space:]]//g`
}
while getopts "s:p:m:k:o:O:g:G:r:z:" arg; do
case "$arg" in
s)
server="$OPTARG"
;;
p)
server_port="$OPTARG"
;;
k)
base64_str="$OPTARG"
password1="$OPTARG"
base64_encode
password="$base64_res"
;;
m)
method="$OPTARG"
;;
o)
obfs="$OPTARG"
;;
O)
protocol="$OPTARG"
;;
g)
base64_str="$OPTARG"
base64_encode
obfs_param="$base64_res"
;;
G)
base64_str="$OPTARG"
base64_encode
protocol_param="$base64_res"
;;
r)
base64_str="$OPTARG"
remark1="$OPTARG"
base64_encode
remark="$base64_res"
;;
z)
base64_str="$OPTARG"
base64_encode
method="$base64_res"
;;
esac
done
if [ "$protocol"X = "XXXXXXXXXXXXXXXXX" ] ; then
base64_str="$method:$password1@$server:$server_port#remark1"
base64_encode
echo "ss://"$base64_res >>ssr.ini
else
base64_str=$server:$server_port:$protocol:$method:$obfs:$password"/?obfsparam="$obfs_param"&protoparam="$protocol_param"&remarks="$remark"&method="$method
base64_encode
echo "ssr://"$base64_res >>ssr.ini
fi

SETSSR
chmod 755 /tmp/setssr.sh

NAME=shadowsocksr
name=shadowsocksr
ssr_group="BEST"
ssr_type="ssr"

uci_get_by_name() {
	local ret=$(uci get $NAME.$1.$2 2>/dev/null)
	echo ${ret:=$3}
}

uci_get_by_type() {
	local ret=$(uci get $NAME.@$1[0].$2 2>/dev/null)
	echo ${ret:=$3}
}

Server_Update() {
	local uci_set="uci -q set $name.$1."
	${uci_set}alias="[$ssr_group] $ssr_remarks"
	${uci_set}auth_enable="0"
	${uci_set}switch_enable="1"
	${uci_set}type="$ssr_type"
	${uci_set}server="$ssr_host"
	${uci_set}server_port="$ssr_port"
	${uci_set}local_port="1234"
	uci -q get $name.@servers[$1].timeout >/dev/null || ${uci_set}timeout="60"
	${uci_set}password="$ssr_passwd"
	${uci_set}encrypt_method="$ssr_method"
	${uci_set}protocol="$ssr_protocol"
	${uci_set}protocol_param="$ssr_protoparam"
	${uci_set}obfs="$ssr_obfs"
	${uci_set}obfs_param="$ssr_obfsparam"
	${uci_set}fast_open="0"
	${uci_set}kcp_enable="0"
	${uci_set}kcp_port="0"
	${uci_set}kcp_param="--nocomp"
}


b64decode() {
	local d="====" data=$(echo $1 | sed 's/_/\//g; s/-/+/g')
	local mod4=$((${#data}%4))
	[ $mod4 -gt 0 ] && data=$data${d:mod4}
	echo $data | base64 -d 2>/dev/null
}


decode_base64str()
{
a1=`echo "$1"|awk -F '#' '{print $1}'`
SSR=$(b64decode $a1)
#存在@而不/?obfsparam=时为ss://
TT="0"
bb=`echo "$SSR"|grep @`
if [ ! "$bb" == "" ]; then
aa=`echo "$bb"|grep obfsparam=`
if [  "$aa" == "" ]; then
TT="1"
fi
fi

if [ "$TT" == "1" ]  ; then
a1=`echo $SSR|awk -F '@' '{print $1}'`
a2=`echo $SSR|awk -F '@' '{print $2}'`
method=`echo $a1|awk -F ':' '{print $1}'`
password=`echo $a1|awk -F ':' '{print $2}'`
server=`echo $a2|awk -F ':' '{print $1}'`
server_port=`echo $a2|awk -F ':' '{print $2}'`

#不加入ss支持   echo $server:$server_port:$password:$method>>ss.ini
SS0="$server:$server_port:$password:$method"
else

server=`echo $SSR|awk -F ':' '{print $1}'`
server_port=`echo $SSR|awk -F ':' '{print $2}'`
protocol=`echo $SSR|awk -F ':' '{print $3}'`
method=`echo $SSR|awk -F ':' '{print $4}'`
obfs=`echo $SSR|awk -F ':' '{print $5}'`

if [ ! "$server" = "" ]  ; then
other=`echo $SSR|awk -F ':' '{print $6}'`
password=`echo $other|awk -F '/' '{print $1}'`
OO=$(b64decode $password)
OOO=`echo $OO|awk -F ':' '{print $2}'`
if [ "$OOO" = "" ] ; then
  password="$OO"
else
  password="BaSe64"$password
fi

OO=`echo "$other"|awk -F 'obfsparam=' '{print $2}'`
OO=`echo "$OO"|awk -F '&' '{print $1}'`
obfsparam=""
if [ ! "$OO" = "" ] ; then
obfsparam=$(b64decode $OO)
fi

OO=`echo "$other"|awk -F 'protoparam=' '{print $2}'`
OO=`echo "$OO"|awk -F '&' '{print $1}'`
protoparam=""
if [ ! "$OO" = "" ] ; then
protoparam=$(b64decode $OO)
fi

ss_usage=""
[ ! "$protocol" = "" ] && ss_usage=$ss_usage" -O $protocol"
[ ! "$obfs" = "" ] && ss_usage="$ss_usage"" -o $obfs"
[ ! "$protoparam" = "" ] && ss_usage="$ss_usage"" -G $protoparam"
[ ! "$obfsparam" = "" ] && ss_usage="$ss_usage"" -g $obfsparam"

[ ! '$ss_usage' = '' ] && ss_usage=${ss_usage:1:1024}
echo $server:$server_port:$password:$method:$ss_usage>>ss.ini
SS0="$server:$server_port:$password:$method:$ss_usage"
fi

fi
}


get_ssr_from_webstr()
{
rm ss.txt 2>/dev/null
curl -o ss.txt  -k -L -s  -m 30 $2 2>/dev/null
[ ! -s ss.txt ] && curl -o ss.txt  -k -L  -s -m 30 $2 2>/dev/null
if [  -s ss.txt ] ; then 
runstr="grep \"$1\" ss.txt|sed 's/[ |<|\"|>]/\n/g' |awk -F \"$1\" '{print \$2}'>sss.txt"
eval "$runstr"
TTT=""
echo "webstr="$2
cat sss.txt | while read TMP
do 
[ "$TMP" = "" ] && continue
[ ${#TMP} -lt 10 ] && continue
if [ ! "$TTT" = "$TMP" ] ; then 
decode_base64str $TMP
TTT=$TMP
fi
done
fi
}


decode_ssrfile()
{
rm ss.txt 2>/dev/null
curl -o ss.txt -s -k -L   -m 30 $1 2>/dev/null
[ ! -s ss.txt ] && curl -o ss.txt -s -k -L -m 30 $1  2>/dev/null
if [  -s ss.txt ] ; then 
#sed -i 's/.*/&==/' ss.txt
#sed -i 's/\r//g' ss.txt
#sed -i 's/\n//g' ss.txt
base64 -d ss.txt  >ssr.txt 2>/dev/null
cat ssr.txt  | while read SSR 
do
[ "${SSR:0:4}" = "MAX=" ] && continue
if [ "${SSR:0:5}" = "ss://" ] ; then
decode_str=${SSR:5:1024}
else
decode_str=${SSR:6:1024}

fi
[ ! "$decode_str" = "" ] && decode_base64str $decode_str
done
fi	
}


########################  get from ishadowsock ########################
get_from_ishadowsock()
{
iss="https://my.ishadowx.net/"
rm ss.txt > /dev/null 2>&1
curl -o ss.txt -s -k   -L  -m 30 $iss 2>/dev/null
iss="https://ss.ishadowx.com/"
[ -s ss.txt ]  &&  curl -o ss.txt -s -k -L   -m 30 $iss 2>/dev/null

if [ -s ss.txt ] ; then
cp /dev/null  ssss.ini
Server=""
Port=""
Pass=""
Method=""
Other=""

grep -E "<h4>IP Address|<h4>Port|<h4>Password|<h4>Method|<h4>auth_" ss.txt | sed 's/<[^<>]*>//g' | sed 's/：/:/g' | sed 's/IP Address/Server/g'| sed 's/ //g' |sed 's/\r//g' | while read i  
do
var1=`echo $i|awk -F ':' '{print $1}'`
var2=`echo $i|awk -F ':' '{print $2}'`
case $var1 in
    Server)  Server="$var2"
    ;;
    Port)  Port="$var2"
    ;;
    Password)  Pass="$var2"
    ;;
    Method)  Method="$var2"
    ;;
    *)  Other="$var1"
    ;;

esac

if [ ! "$Server" = "" ]  && [ ! "$Port" = "" ]  && [ ! "$Method" = "" ]  ; then
    [ ! "$Pass" = "" ]  && echo $Server:$Port:$Pass:$Method >>ss.ini
    Server=""
    Port=""
    Pass=""
    Method=""
    Other=""
fi
done
echo "==========" >> ss.ini 
fi

}


get_temp()
{
###临时

if [ "X" == "X" ] ; then
rm ss.txt 2>/dev/null
curl -o ss.txt -s -k -L   -m 30 https://www.ssrtool.com/tool/free_ssrC 2>/dev/null
[ ! -s ss.txt ] && curl -o ss.txt  -s -k -L   -m 30 https://www.ssrtool.com/tool/free_ssr 2>/dev/null
if [  -s ss.txt ] ; then 
cat ss.txt|grep 'onclick="showLink' >a.html

echo "###https://www.ssrtool.com/tool/free_ssrC" >>ss.ini
sed "s/'//g" a.html|while read str
do
OO=`echo "$str"|awk -F 'showLink' '{print $2}'`
#echo $OO
OO=`echo "$OO"|awk -F ')' '{print $1}'`
OO=${OO:1:1000}

Server=`echo "$OO"|awk -F ',' '{print $1}'`
Port=`echo "$OO"|awk -F ',' '{print $2}'`

Password=`echo "$OO"|awk -F ',' '{print $3}'`
O0=`echo "$Password"|awk -F ':' '{print $2}'`
[ ! "$O0" = "" ] && Password="BaSe64"$(b64decode $Password)

Method=`echo "$OO"|awk -F ',' '{print $4}'`
O0=`echo "$OO"|awk -F ',' '{print $5}'`
G0=`echo "$OO"|awk -F ',' '{print $6}'`
O1=`echo "$OO"|awk -F ',' '{print $7}'`
G1=`echo "$OO"|awk -F ',' '{print $8}'`
[ ! "$O0" = "" ] && O0=" -O "$O0
[ ! "$O1" = "" ] && O1=" -o "$O1
[ ! "$G0" = "" ] && G0=" -G "$G0
[ ! "$G1" = "" ] && G1=" -g "$G1
ss_usage="$O0$O1$G0$G1"
ss_usage=${ss_usage:1:1000}
echo $Server:$Port:$Password:$Method:$ss_usage >>ss.ini
done
fi
fi



###临时

rm ss.txt 2>/dev/null
curl -o ss.txt -s -k -L   -m 30  https://www.ssrtool.com/tool/free_ssr 2>/dev/null
[ ! -s ss.txt ] && curl -o ss.txt  -s -k -L   -m 30  https://www.ssrtool.com/tool/free_ssr 2>/dev/null
if [  -s ss.txt ] ; then 
echo "###https://www.ssrtool.com/tool/free_ssr" >>ss.ini

OO=`cat ss.txt|grep getGolSub`
OO=`echo "$OO"|awk -F 'value="' '{print $2}'`
OO=`echo "$OO"|awk -F '"' '{print $1}'`


decode_ssrfile $OO
fi

}



####### 以下为必集所有的结点
###加入之前的配置
curl -o ss.txt -s -m 30 $serverurl"ss.ini"	
if [ $? = 0 ] ; then
  head -n 40  ss.txt >ss.ini
  echo "==============" >>ss.ini
fi


#get_from_ishadowsock
#echo "###get_temp" >>ss.ini
#get_temp


######加入收集的配置
curl -o ss.txt -s -l -k  -m 30 https://gitee.com/yzqiang/autoss/raw/master/ss.txt	
if [ $? = 0 ] ; then
tr -d "\015" <ss.txt | while read str
do
  
  if [ "${str:0:6}" = "ssr://" -o "${str:0:5}" = "ss://" ]   ; then
    decode_str=${str:6:1024}
    [ ${#decode_str} -gt 20 ]  &&  decode_base64str  
  else
    if [ "${str:0:4}" = "http" ] ; then  
		echo "####sub:"$str >>ss.ini    
		decode_ssrfile $str	
	else
		if [ "${str:0:4}" = "web|" ] ; then  
			webstr="`echo "$str"|awk -F '|' '{print $3}'`"	
			deli="`echo "$str"|awk -F '|' '{print $2}'`"   	   
			echo "####web:"$webstr >>ss.ini            
			get_ssr_from_webstr $deli $webstr   
		else	  
			[ ${#str} -gt 20 ]  &&  [ ! "$str" = "" ] && [ ! "${str:0:1}" = "#" ] && echo $str >>ss.ini  
		fi
	fi
  fi
done 
fi
######加入收集的配置  end
#######################以上为收集所有的结点


[ ! -s ss.ini ] && exit 1
cp ss.ini ssss.ini
sed 's/^[ \t]*//g;s/[ \t]*$//g;/^$/d;/^[#=]/d' ss.ini>ss.txt && mv ss.txt ss.ini
[ ! -s ss.ini ] && exit 1

###################### set ss information ####################################

############以下为测速
if [ "aaa" == "aaa" ] ; then
###############检查SS可用性

cat /tmp/cron_ss.lock >cron_ss.lock
rm ssr.txt >/dev/null 2>&1
logger "get bestss server"
options1=""
options2=""
ss_usage=""
ss_usage_json=""
rm ss.txt >/dev/null 2>&1
rm ssr.inf >/dev/null 2>&1
rm ssr.ini >/dev/null 2>&1
rm ss.inf >/dev/null 2>&1
rm s.inf >/dev/null 2>&1

CC=1
BESTTIME=0
BT=0
BESTTIME0=90   #两秒内的成功个数
BT0=30        	#一秒内的成功个数  
CC0=500			#扫描条数限制
[ $(date +%H) -ge 7 ] && BESTTIME0=30 && BT0=10  && CC0=200        #七点钟之后减少扫描个数

HOST1=""
HOST0=""

monitor=$(ps|grep /usr/bin/ssr-monitor|grep -v grep) 
monitor_id=$(echo "$monitor"| awk -F ' ' '{print $1}')
monitor="${monitor##*/bin/sh}"
switch=$(ps|grep /usr/bin/ssr-switch|grep -v grep) 
switch_id=$(echo "$switch"| awk -F ' ' '{print $1}')
switch="${switch##*/bin/sh}"
[ ! "$monitor_id" == "" ] && kill -9 $monitor_id 
[ ! "$switch_id" == "" ] && kill -9 $switch_id 

sort ss.ini | while read str
do
[ $CC -ge $CC0 -o $BESTTIME -ge $BESTTIME0 -o $BT -ge $BT0 ] && break

[ "$str" = "" ] && continue
[ ${str:0:1} = "#" ] && continue 
[ ${str:0:1} = "=" ] && continue 

ss_s1_ip=`echo $str|awk -F ':' '{print $1}'`  
[ ${#ss_s1_ip} = 1 ] && continue
[ ${#ss_s1_ip} = 2 ] && continue
[ ${#ss_s1_ip} = 3 ] && continue
ss_s1=$ss_s1_ip
ss_s1_port=`echo $str|awk -F ':' '{print $2}'` 
[ $ss_s1_port"X" == "X" ] && continue
[ "${ss_s1_port:0:1}" \< "0" ] && continue 
[ "${ss_s1_port:0:1}" \> "9" ] && continue 

[ "$ss_s1_ip" = "$HOST1" ] && continue 
[ "$ss_s1_ip" == "www.google.com" ] && continue 
[ "$HOST0" = $ss_s1_ip":"$ss_s1_port ] && continue 
HOST0=$ss_s1_ip":"$ss_s1_port

ss_s1_key=`echo $str|awk -F ':' '{print $3}'`
ss_s1_key0=$ss_s1_key
ss_s1_key=${ss_s1_key//：/:}
[ "${ss_s1_key:0:6}" = "BaSe64" ] && ss_s1_key=$(b64decode ${ss_s1_key:6:1000})

ss_s1_method=`echo $str|awk -F ':' '{print $4}'` 

tmp=`echo $str|awk -F ':' '{print $5}'`  
[ ! "$tmp" = "" ] && SS="echo '$str'|awk -F ':$ss_s1_method:' '{print \$2}'" && ss_usage=`eval "$SS" | sed -r 's/\--[^ ]+[^-]+//g'`  || ss_usage=""  
ss_usage0=$ss_usage
[ "${ss_usage:0:6}" = "BaSe64" ] && ss_usage=$(b64decode ${ss_usage:6:1000})

ss_server0=$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method:$ss_usage
ss_server1=$ss_s1_ip
resolveip=`resolveip -4 -t 4 $ss_server1 | grep -v : | sed -n '1p'`
if [ ! -n "$resolveip" ] ; then
	TMP=`ping -c 1 -4 $ss_server1 2>/dev/null | grep "PING "`
	OO=`echo "$TMP"|awk -F '(' '{print $2}'`
	OO=`echo "$OO"|awk -F ')' '{print $1}'`
	resolveip=$OO
fi

ss_s1_ip=$ss_server1
[ "$ss_s1_ip" = "" ] && continue

[ ! -z "$ss_usage" ] && ss_usage=`echo "$ss_usage" | sed -e "s/ -. -/ -/g"| sed -e "s/ -.$//g"`

/tmp/SSJSON.sh  -f  /tmp/ss.json $ss_usage $ss_usage_json -s $ss_s1_ip -p $ss_s1_port -l 1234 -b 0.0.0.0 -k $ss_s1_key -m $ss_s1_method
pidof ssr-redir  >/dev/null 2>&1 && killall ssr-redir && killall -9 ssr-redir 2>/dev/null
/usr/bin/ssr-redir -c /tmp/ss.json -f /var/run/ssr-retcp_1.pid >/dev/null 2>/dev/null
action_ssip=$ss_s1_ip
BP_IP="$action_ssip"
#url="https://www.youtube.com/intl/zh-CN/yt/about/"
rm /tmp/tmp.txt 2>/dev/null
starttime=$(cat /proc/uptime | cut -d" " -f1)
for i in $(seq 1 5)
do 
curl -o /tmp/tmp.txt  -s -k -L -r 0-39999 -m 2  $url 2>/dev/null 
CODE="$?"
endtime=$(cat /proc/uptime | cut -d" " -f1)
TIME=`awk -v x=$starttime -v y=$endtime 'BEGIN {printf y-x}'`

if [  "$CODE" == "28" ] ; then 
	if  [  -s /tmp/tmp.txt ] ; then
		CODE="0"
		endtime=$(wc -c /tmp/tmp.txt | cut -d" " -f1)
		TIME=`awk  -v y=$endtime 'BEGIN {printf 2*i+(39999-y)/20000}'`
		TIME=${TIME:0:4}
		[ "${TIME:0:1}" == "-" ] && TIME="9.99" && CODE="28"
	fi
fi
[  "$CODE" == "0" ] && break  
TIME0=`awk -v x=$TIME 'BEGIN{printf "%.2f\n",x-0.01}'`
[   "${TIME0:0:1}" != "0" -a  "${TIME0:0:1}" != "1"  ] && break 
done



TIME0=`awk -v x=$TIME 'BEGIN{printf "%.2f\n",x-0.01}'`

if [  "$CODE" == "0" ] ; then
  [ "${TIME0:0:1}" \< "2.0" ] && let BESTTIME=$BESTTIME+1
	[ "${TIME0:0:3}" \< "1.0" ] && let BT=$BT+1
	
  [ $CC -ge 10 ] && echo "$CC $TIME0 $ss_server0" && logger "$CC $TIME0 $ss_server0"
  [ $CC -lt 10 ] && echo "0$CC $TIME0 $ss_server0" && logger "0$CC $TIME0 $ss_server0"
	RES=`awk -v a=$TIME  'BEGIN { print (a<=10)?1:0'}`
	[ "$RES" == "1"  ] && let CC=$CC+1	&& echo "$TIME0:$ss_s1:$ss_s1_port:$ss_s1_key0:$ss_s1_method:$ss_usage0" >>ss.txt
else
	HOST1=$ss_s1
	echo "XX" $TIME0 "$ss_server0" $CODE
	logger "XX" $TIME0 "$ss_server0" $CODE
fi



done
SUCESS=1
else
###############不检查SS可用性
SUCESS=0
fi
############以上为测速

if [ ! -s ss.txt ] ; then
  /etc/init.d/shadowsocksr restart
  curl -o ss.inf -s -m 10 $serverurl"ss.ini"
  head -n 2 ss.inf | sed '1s/^/1.00:&/g'  | sed '2s/^/2.00:&/g'  >ss.txt
  SUCESS=0
else
  CC=1  
  if [ "$(uci_get_by_type global threads 0)" = "0" ] ;then
	threads=$(cat /proc/cpuinfo | grep 'processor' | wc -l)
  else
	threads=$(uci_get_by_type global threads)
  fi
	
  sort ss.txt|head -n 20 >ss.inf
  
  cat ss.inf | while read str
  do  
    TIME=`echo "$str"|awk -F ':' '{print $1}'`  
    ss_s1_ip=`echo "$str"|awk -F ':' '{print $2}'`  
    ss_s1_port=`echo "$str"|awk -F ':' '{print $3}'`  
    ss_s1_key=`echo "$str"|awk -F ':' '{print $4}'`  
	ss_s1_key=${ss_s1_key//：/:}
    [ "${ss_s1_key:0:6}" = "BaSe64" ] && ss_s1_key=$(b64decode ${ss_s1_key:6:1000})

    ss_s1_method=`echo $str|awk -F ':' '{print $5}'`  
	tmp=`echo $str|awk -F ':' '{print $6}'`  
	ss_usage=""
    [ ! "$tmp" = "" ] && SS="echo '$str'|awk -F ':$ss_s1_method:' '{print \$2}'" && ss_usage=`eval "$SS" | sed -r 's/\--[^ ]+[^-]+//g'`    
    [ "${ss_usage:0:6}" = "BaSe64" ] && ss_usage=$(b64decode ${ss_usage:6:1000})

    /tmp/setssr.sh -r $CC -z ssr -s $ss_s1_ip -p $ss_s1_port -m $ss_s1_method -k $ss_s1_key $ss_usage 
    if [ $CC -le $threads ] ; then	
		echo "The No$CC server: "$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method"   "$TIME
		echo "The No$CC server: "$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method"   "$TIME >serverinfo
		logger "The No$CC server: "$ss_s1_ip:$ss_s1_port:$ss_s1_key:$ss_s1_method"   "$TIME
    fi
	ssr_remarks="$CC"
	ssr_host="$ss_s1_ip"
	ssr_port="$ss_s1_port"
	ssr_passwd="$ss_s1_key"
	ssr_method="$ss_s1_method"
	ssr_protocol="origin"
	ssr_protoparam=""
	ssr_obfs="plain"
	ssr_obfsparam=""	

	if [ ! -z "$ss_usage" ] ; then
		ss_usage="`echo -n " $ss_usage" | sed -e "s@  @ @g" | sed -e "s@  @ @g" | sed -e "s@  @ @g" | sed -e "s@  @ @g"`"		

		sss="${ss_usage##* -O }"
		sss="${sss%% -*}"
		sss="`echo -n "$sss" | sed -e "s@ @@g" `"
		[ ! "$sss" == "" ] && ssr_protocol="$sss"
	
		sss="${ss_usage##* -o }"
		sss="${sss%% -*}"
		sss="`echo -n "$sss" | sed -e "s@ @@g" `"
		[ ! "$sss" == "" ] && ssr_obfs="$sss"
	
		sss="${ss_usage##* -G }"
		sss="${sss%% -*}"
		sss="`echo -n "$sss" | sed -e "s@ @@g" `"
		[ ! "$sss" == "" ] && ssr_protoparam="$sss"
	
		sss="${ss_usage##* -g }"
		sss="${sss%% -*}"
		sss="`echo -n "$sss" | sed -e "s@ @@g" `"
		[ ! "$sss" == "" ] && ssr_obfsparam="$sss"	
	fi

	
	uci_name_tmp=$(uci show $name | grep -w "\[$ssr_group\] $ssr_remarks" | awk -F . '{print $2}')
	if [ -z "$uci_name_tmp" ]; then
		uci_name_tmp=$(uci add $name servers)
	fi

	Server_Update $uci_name_tmp
	uci commit $name		
    let CC=$CC+1
  done
fi

rm -f cron_ss.lock 2>/dev/null

if [  -s ssr.ini ] ; then
#  serverurl="http://ggcloud.tk/mac/"
	head -n 20 ssr.ini |  base64   | sed ":a;N;s/\n//g;ta" >ssr.txt
	fn="openwrt"
	echo "openwrt" >$fn

	cat serverinfo >>$fn	
	rm serverinfo

	cut ss.inf -c6-600 | head -n 30 >s.inf	
	[ -s $fn ] && curl $serverurl"getfile.php?fn=inf/$fn" -F "file=@$fn"
	[ -s ssr.txt ] && curl $serverurl"getfile.php?fn=ssr.txt" -F "file=@ssr.txt"
	[ -s s.inf ] && curl $serverurl"getfile.php?fn=ss.ini" -F "file=@s.inf" 
fi


#url="https://www.youtube.com/intl/zh-CN/yt/about/"
/etc/init.d/shadowsocksr stop 2>/dev/null
sleep 2
/etc/init.d/shadowsocksr start  2>/dev/null
sleep 7
for i in $(seq 1 10)  
do
rm /tmp/tmp.txt 2>/dev/null
curl -o /tmp/tmp.txt -s -k -L --retry 3 -r 0-10239 -m 5 $url 2>/dev/null
[  -s /tmp/tmp.txt  ] && exit 0
sleep 2
done
echo 
echo "fail to SS"
logger "fail to SS"
/etc/init.d/shadowsocksr stop 2>/dev/null
sleep 2
/etc/init.d/shadowsocksr start  2>/dev/null




